package com.draemoncode.weixindb.event;

import com.draemoncode.weixindb.constant.HongBaoFieldConstant;
import com.draemoncode.weixindb.constant.MessagePackageTypeConstant;
import com.draemoncode.weixindb.conventer.CMessagePackageConventer;
import com.draemoncode.weixindb.domain.B_HongBao;
import com.draemoncode.weixindb.domain.B_Member;
import com.draemoncode.weixindb.repository.B_HongBaoRepository;
import com.draemoncode.weixindb.repository.B_MemberRepository;
import com.draemoncode.weixindb.server.CMessagePackage;
import com.draemoncode.weixindb.server.IEvent;
import com.draemoncode.weixindb.service.BusiService;
import com.draemoncode.weixindb.util.ValueUtil;
import com.draemoncode.weixindb.websocket.UIMessage;
import com.draemoncode.weixindb.websocket.UISession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 第2步 下注
 * @author yangz
 * @date 2017/4/9
 *
 */
@Component
@Slf4j
public class HongBaoEvent implements IEvent<List<B_HongBao>, Object> {

    @Autowired
    private BusiService busiService;

    @Autowired
    private B_MemberRepository b_memberRepository;

    @Autowired
    private B_HongBaoRepository b_hongBaoRepository;

    @Override
    public Boolean shoudProcess(CMessagePackage b_csmessage) {
        if(MessagePackageTypeConstant.REQUEST_QIANG_HONG_BAO.equals(b_csmessage.getType())){
            return true;
        }
        return false;
    }

    @Override
    public List<B_HongBao> busiExec(CMessagePackage cMessagePackage) {
        List<Map<String, Object>> list = (List<Map<String, Object>>) cMessagePackage.getMsg().get(HongBaoFieldConstant.REQUEST_HONG_BAO);
        List<B_HongBao> result = new ArrayList<>();
        for (Map<String, Object> item : list) {
            String name = ValueUtil.toString(item.get(HongBaoFieldConstant.REQUEST_USER_NAME));
            BigDecimal money = ValueUtil.toBigDecimal(item.get(HongBaoFieldConstant.REQUEST_MONEY));
            B_Member b_member = b_memberRepository.findByName(name);
            B_HongBao b_hongBao = new B_HongBao();
            b_hongBao.setMoney(money);
            b_hongBao.setMember_id(b_member.getId());
            b_hongBao.setMember_name(b_member.getName());
            b_hongBao.setPod_id(cMessagePackage.getSyncid());
            b_hongBao.setMoney(money);
            B_HongBao save = b_hongBaoRepository.save(b_hongBao);
            result.add(save);
        }
        return result;
    }

    @Override
    public Object uiExec(CMessagePackage cMessagePackage, List<B_HongBao> busiResult) {
        UIMessage uiMessage = CMessagePackageConventer.toUIMessage(cMessagePackage);
        uiMessage.getMsg().clear();
        uiMessage.setPodId(cMessagePackage.getSyncid());
        UISession.send(uiMessage);
        return null;
    }

    @Override
    public CMessagePackage buildResponse(CMessagePackage cMessagePackage, List<B_HongBao> busiResult, Object uiResult) {
        CMessagePackage response = new CMessagePackage(MessagePackageTypeConstant.RESPONSE_RECEIVE);
        response.setSyncid(busiResult.get(0).getPod_id());
        return response;
    }

}
